/**
 * 生成分页
 */
function generateCheckPage() {
	// 获取分页数据
	var pageInfo = getPageInfoRemote();
	// 填充表格
	fillTableBody(pageInfo);
}

/**
 * 获取medicine数据
 * @returns {null|*}
 */
function getPageInfoRemote() {
	// ajax请求的返回值
	var ajaxResult = $.ajax({
		"url": "check/get/page.json",
		"type": "post",
		"data": {
			"pageNum": window.pageNum,
			"pageSize": window.pageSize,
			"checkName": window.checkName
		},
		"dataType": "json",
		"async": false
	});
	var statusCode = ajaxResult.status;
	// 服务器请求失败
	if (statusCode != 200){
		layer.msg("请求失败！状态码：[" + statusCode + "]，说明：[" + ajaxResult.statusText + "]");
		return null;
	}
	var resultEntity = ajaxResult.responseJSON;
	// 逻辑错误
	if ("failed" == resultEntity.code){
		layer.msg("请求错误！说明：[" + resultEntity.msg + "]");
		return null;
	}
	// 请求成功
	return resultEntity.data;
}

/**
 * 填充表格
 * @param pageInfo
 */
function fillTableBody(pageInfo) {
	var tbody = $("tbody");
	// 清除原本<tbody>和分页条中的原数据
	tbody.empty();
	$("#Pagination").empty();
	// 判断pageInfo是否有效
	if (pageInfo == null || pageInfo.list == null || pageInfo.list.length == 0){
		tbody.append("<tr><td colspan='8' align='center'>没有查询到数据！</td></tr>");
		return;
	}
	// 填充<tbody>
	for (var i = 0;i < pageInfo.list.length;i++){
		var check = pageInfo.list[i];
		// 要显示的第一列：序号
		var serialNumber = (pageInfo.pageNum - 1) * pageInfo.pageSize + i + 1;
		var checkId = check.checkId;
		var checkName = check.checkName;
		var temperature = check.temperature;
		var bloodPressure = check.bloodPressure;
		var bloodSugar = check.bloodSugar;
		var pulse = check.pulse;
		var checkTime = check.checkTime;
		var numberTd = "<td>" + serialNumber + "</td>";
		var checkNameTd = "<td>" + checkName + "</td>";
		var temperatureTd = "<td>" + temperature + "</td>";
		var bloodPressureTd = "<td>" + bloodPressure + "</td>";
		var bloodSugarTd = "<td>" + bloodSugar + "</td>";
		var pulseTd = "<td>" + pulse + "</td>";
		var checkTimeTd = "<td>" + checkTime + "</td>";
		// 按钮组
		var removeButton = "<button type='button' checkId='" + checkId + "' class='removeCheckBtn btn btn-danger btn-xs'><i class='glyphicon glyphicon-remove'></i></button>";
		var buttonTd = "<td>" + removeButton + "</td>";
		// 手动渲染
		var tr = "<tr>" + numberTd + checkNameTd + temperatureTd + bloodPressureTd + bloodSugarTd + pulseTd + checkTimeTd + buttonTd + "</tr>"
		tbody.append(tr);
	}
	// 生成分页条
	generateNav(pageInfo);
}

/**
 * 初始化分页条
 * @param pageInfo
 */
function generateNav(pageInfo) {
	// 获取总记录数
	var totalRecord = pageInfo.total;
	// 声明pagination属性
	var properties = {
		// 边缘页数
		num_edge_entries: 3,
		// 主体页数
		num_display_entries: 5,
		// 回调函数
		callback: paginationCallBack,
		// 每页显示的条数
		items_per_page: window.pageSize,
		// 当前显示的页面
		current_page: window.pageNum - 1,
		// 上一页与下一页按钮上显示的文本
		prev_text: "上一页",
		next_text: "下一页"
	};
	// 生成页码导航条
	$("#Pagination").pagination(totalRecord,properties);
}

/**
 * pagination回调函数
 * @param pageIndex
 * @param jQuery
 */
function paginationCallBack(pageIndex,jQuery) {
	// 翻页
	window.pageNum = pageIndex + 1;
	// 调用分页函数
	generateCheckPage();
	return false;
}
